package cn.qzuie.smart.module.dorm.controller.admin.energyusage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.energyusage.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.energyusage.EnergyUsageDO;
import cn.qzuie.smart.module.dorm.service.energyusage.EnergyUsageService;

@Tag(name = "管理后台 - 能源使用")
@RestController
@RequestMapping("/dorm/energy-usage")
@Validated
public class EnergyUsageController {

    @Resource
    private EnergyUsageService energyUsageService;

    @PostMapping("/create")
    @Operation(summary = "创建能源使用")
    @PreAuthorize("@ss.hasPermission('dorm:energy-usage:create')")
    public CommonResult<Long> createEnergyUsage(@Valid @RequestBody EnergyUsageSaveReqVO createReqVO) {
        return success(energyUsageService.createEnergyUsage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新能源使用")
    @PreAuthorize("@ss.hasPermission('dorm:energy-usage:update')")
    public CommonResult<Boolean> updateEnergyUsage(@Valid @RequestBody EnergyUsageSaveReqVO updateReqVO) {
        energyUsageService.updateEnergyUsage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除能源使用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:energy-usage:delete')")
    public CommonResult<Boolean> deleteEnergyUsage(@RequestParam("id") Long id) {
        energyUsageService.deleteEnergyUsage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除能源使用")
                @PreAuthorize("@ss.hasPermission('dorm:energy-usage:delete')")
    public CommonResult<Boolean> deleteEnergyUsageList(@RequestParam("ids") List<Long> ids) {
        energyUsageService.deleteEnergyUsageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得能源使用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:energy-usage:query')")
    public CommonResult<EnergyUsageRespVO> getEnergyUsage(@RequestParam("id") Long id) {
        EnergyUsageDO energyUsage = energyUsageService.getEnergyUsage(id);
        return success(BeanUtils.toBean(energyUsage, EnergyUsageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得能源使用分页")
    @PreAuthorize("@ss.hasPermission('dorm:energy-usage:query')")
    public CommonResult<PageResult<EnergyUsageRespVO>> getEnergyUsagePage(@Valid EnergyUsagePageReqVO pageReqVO) {
        PageResult<EnergyUsageDO> pageResult = energyUsageService.getEnergyUsagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EnergyUsageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出能源使用 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:energy-usage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnergyUsageExcel(@Valid EnergyUsagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EnergyUsageDO> list = energyUsageService.getEnergyUsagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "能源使用.xls", "数据", EnergyUsageRespVO.class,
                        BeanUtils.toBean(list, EnergyUsageRespVO.class));
    }

}